package com.nand.blockdemo.ui.view;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.impl.PartShadowPopupView;
import com.nand.blockdemo.R;
import com.nand.blockdemo.adapter.MenuAdapter;
import com.nand.blockdemo.bean.MenuBean;

import java.util.List;


public class DropDownMenuPopupView extends PartShadowPopupView {

    private Context context;
    private List<MenuBean> list;
    private MaxHeightRecyclerView mRecyclerView;
    private OnItemClickListener onItemClickListener;
    private MenuAdapter menuAdapter;


    public DropDownMenuPopupView(@NonNull Context context, List<MenuBean> list) {
        super(context);
        this.context = context;
        this.list = list;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_drop_down_menu;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        //获取资源对象
        Resources resources = getResources();
        //获取屏幕数据
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        //获取屏幕宽高，单位是像素
        int heightPixels = displayMetrics.heightPixels;

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setMaxHeight(heightPixels / 2);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        menuAdapter = new MenuAdapter(R.layout.item_menu_layout, list);
        mRecyclerView.setAdapter(menuAdapter);
        menuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (onItemClickListener != null) {
                    for (int i = 0; i < adapter.getData().size(); i++) {
                        if(i == position){
                            ((MenuBean)adapter.getData().get(i)).setSelect(true);
                        }else {
                            ((MenuBean)adapter.getData().get(i)).setSelect(false);
                        }
                    }
                    adapter.notifyDataSetChanged();
                    onItemClickListener.onItemClick(adapter, view, position);
                }
            }
        });
    }

    public void noSelect(){
        for (int i = 0; i < menuAdapter.getData().size(); i++) {
            MenuBean item = menuAdapter.getData().get(i);
            item.setSelect(false);
        }
        menuAdapter.notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
//        void onItemClick(MenuBean data, View view, int position);
        void onItemClick(BaseQuickAdapter adapter, View view, int position);
    }
}
